package com.fenghongzhang.music;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fenghongzhang.music.adapter.WelcomeAdapter;
import com.fenghongzhang.music.fragment.OneFragment;
import com.fenghongzhang.music.fragment.TwoFragment;
import com.fenghongzhang.music.fragment.WelcomeFragment;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    private ViewPager vp;
    private LinearLayout ll;


    private List<Fragment> list;

    private WelcomeFragment welcomeFragment ;

    //小圆点结合
    private List<ImageView> imageViewList;

    private static final String TAG = "WelcomeActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);


        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);

        //判断是否走引导页
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean welcome1 = welcome.getBoolean("welcome", false);
        //直接跳转
        if(welcome1){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        //vp中的fragment的集合
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        welcomeFragment = new WelcomeFragment();
        list.add(welcomeFragment);


        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(welcomeAdapter);


        //根据页面的多少.来添加小圆点
        //添加小圆点集合
        imageViewList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);

            //第一个就是红色
            if(i == 0){
                imageView.setImageResource(R.drawable.cricle_t);
            }else{
                imageView.setImageResource(R.drawable.cricle);
            }

            imageViewList.add(imageView);

            ll.addView(imageView);
        }


        SharedPreferences welcome2 = getSharedPreferences("welcome", MODE_PRIVATE);
        SharedPreferences.Editor edit = welcome2.edit();
        SharedPreferences.Editor welcome3 = edit.putBoolean("welcome", true);
        welcome3.commit();

        //vp的翻页监听
       vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           @Override
           public void onPageSelected(int position) {

               //这里可以判断开始时间->最后一页 , 开启倒计时
               if(position == list.size() -1){
                   welcomeFragment.handler.sendEmptyMessage(100);
               }

               for (int i = 0; i < imageViewList.size(); i++) {
                    //如果是当前页就改小圆点的颜色
                   if(i == position){
                       imageViewList.get(i).setImageResource(R.drawable.cricle_t);
                   }else{
                       imageViewList.get(i).setImageResource(R.drawable.cricle);
                   }

               }

           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });



    }
}
